package com.ng.desktop.fpa.model;

import org.apache.commons.lang.StringUtils;
import org.xBaseJ.DBF;
import org.xBaseJ.xBaseJException;

import com.ng.desktop.fpa.persistence.DBFBuilder;

public class DetalleFacturaDBF implements DBFBuilder<DetalleFacturaDBF> {

	public static final Integer SERIE = 0;
	public static final Integer NUMERO_FACTURA = 1;
	public static final Integer ARTICULO_ID = 2;
	public static final Integer ARTICULO_DESCRIPCION = 3;

	private Long id;

	private String cserie;

	private String nnumfac;

	private String cref;

	private String cdetalle;

	private String factura;

	private String key;

	public DetalleFacturaDBF() {
	}

	public DetalleFacturaDBF(String cserie, String nnumfac, String cref, String cdetalle) {
		super();
		this.cserie = cserie;
		this.nnumfac = StringUtils.isEmpty(nnumfac) ? "-1" : nnumfac;
		this.cref = cref;
		this.cdetalle = cdetalle;
		this.factura = this.cserie + "-" + this.nnumfac;
		this.key = this.cserie + "-" + this.nnumfac + "-" + this.cref;
	}

	public DetalleFacturaDBF build(Object[] objectRow) {

		String cserie = (String) objectRow[DetalleFacturaDBF.SERIE];
		String nnumfac = objectRow[DetalleFacturaDBF.NUMERO_FACTURA] != null ? objectRow[DetalleFacturaDBF.NUMERO_FACTURA]
				.toString() : "";
		int indexOf = nnumfac.indexOf(".");
		if (indexOf != -1) {
			nnumfac = nnumfac.substring(0, indexOf);
		}

		String cref = (String) objectRow[DetalleFacturaDBF.ARTICULO_ID];
		String cdetalle = (String) objectRow[DetalleFacturaDBF.ARTICULO_DESCRIPCION];

		return new DetalleFacturaDBF(cserie.trim(), nnumfac.trim(), cref.trim(), cdetalle.trim());
	}

	@Override
	public DetalleFacturaDBF build(DBF classDB) throws ArrayIndexOutOfBoundsException, xBaseJException {

		String cserie = classDB.getField("cserie").get();
		String nnumfac = classDB.getField("nnumfac").get();
		String cref = classDB.getField("cref").get();
		String cdetalle = classDB.getField("cdetalle").get();

		return new DetalleFacturaDBF(cserie.trim(), nnumfac.trim(), cref.trim(), cdetalle.trim());
	}

	public String getCserie() {
		return cserie;
	}

	public String getNnumfac() {
		return nnumfac;
	}

	public String getCref() {
		return cref;
	}

	public String getCdetalle() {
		return cdetalle;
	}

	public String getFactura() {
		return factura;
	}

	public String getKey() {
		return key;
	}

	public void setCserie(String cserie) {
		this.cserie = cserie;
	}

	public void setNnumfac(String nnumfac) {
		this.nnumfac = nnumfac;
	}

	public void setCref(String cref) {
		this.cref = cref;
	}

	public void setCdetalle(String cdetalle) {
		this.cdetalle = cdetalle;
	}

	public void setFactura(String factura) {
		this.factura = factura;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

}
